package com.jstyle.jclife.adapter;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.jclife.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/6/11.
 */

public class SupportAdapter extends RecyclerViewBaseAdapter<String> {

    final int VIEWTYPE_NORMAL = 0;
    final int VIEWTYPE_OTHER = 1;



    public SupportAdapter(List<String> mDatas) {
        super(mDatas);
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEWTYPE_NORMAL:
                bindNormal(holder, position);
                break;
            case VIEWTYPE_OTHER:
                bindOther(holder);
                break;
        }

    }

    private void bindOther(BaseViewHolder holder) {
        OtherViewHolder normalViewHolder = (OtherViewHolder) holder;
        Context context = normalViewHolder.itemView.getContext();
        normalViewHolder.tvSupport.setText(context.getString(R.string.fomofit_support));
    }

    private void bindNormal(BaseViewHolder holder, final int position) {
        NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
        Context context = normalViewHolder.itemView.getContext();
        normalViewHolder.btSettingPosition.setText(mDataList.get(position));
        // normalViewHolder.viewBottom.setVisibility(position == mDataList.size() - 1 ? View.VISIBLE : View.GONE);
        Drawable drawableRight = context.getResources().getDrawable(R.drawable.user_right);
        if (drawableRight != null) {
            drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());
            normalViewHolder.btSettingPosition.setCompoundDrawables(null, null, drawableRight, null);
        }
        normalViewHolder.tvItemAbout.setVisibility( View.GONE);
    }

    public String getAppVersion(Context context) {
        PackageInfo packageInfo;
        String versionName = "";
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;

    }

    @Override
    public int getItemViewType(int position) {
        int type = VIEWTYPE_NORMAL;
        switch (position) {
            case 0:
                type = VIEWTYPE_OTHER;
                break;
        }
        return type;
    }

    @Override
    protected int getLayoutId(int viewType) {
        int id = R.layout.item_about_us;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                id = R.layout.item_about_us;
                break;
            case VIEWTYPE_OTHER:
                id = R.layout.item_support_title;
                break;
        }
        return id;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = null;
        switch (viewType) {
            case VIEWTYPE_NORMAL:
                View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new NormalViewHolder(view);
                break;
            case VIEWTYPE_OTHER:
                View viewOther = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
                baseViewHolder = new OtherViewHolder(viewOther);
                break;
        }
        return baseViewHolder;
    }

    public class OtherViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_support)
        TextView tvSupport;

        public OtherViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class NormalViewHolder extends BaseViewHolder {
        @BindView(R.id.bt_setting_position)
        Button btSettingPosition;
        @BindView(R.id.tv_item_about)
        TextView tvItemAbout;

        public NormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
